// pages/test/test.js
const app=getApp();
const plugin = requirePlugin('WechatSI');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isShow:true,
    },

    createAudioSrc:function(test){
        plugin.textToSpeech({
            lang:'en_US',
            tts:true,
            content:test.question,
            success:ret=>{
                this.questionAudioSrc=ret.filename
                this.questionAudioCtx.src=this.questionAudioSrc;
                this.questionAudioCtx.play();
            },
            fail:ret=>{
                console.log('fail tts',ret)
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    testList:null,
    currentIndex:0,
    questionAudioSrc:null,
    correctAudioSrc:null,
    wrongAudioSrc:null,
    questionAudioCtx:null,
    correctAudioCtx:null,
    wrongAudioCtx:null,
    user:null,
    onLoad: function (options) {
        let that=this;
        this.user=wx.getStorageSync('user');
        this.setData({userPhoto:this.user.head_Photo,userName:this.user.userName})
        let test_Classify=options.test_Classify;
        this.questionAudioCtx=wx.createInnerAudioContext();
        this.correctAudioCtx=wx.createInnerAudioContext();
        this.wrongAudioCtx=wx.createInnerAudioContext();
        plugin.textToSpeech({
            lang:'en_US',
            tts:true,
            content:`You're great.That's right`,
            success:ret=>{
                that.correctAudioSrc=ret.filename;
                that.correctAudioCtx.src=that.correctAudioSrc;
            }
        })
        plugin.textToSpeech({
            lang:'en_US',
            tts:true,
            content:`Wrong answer,think again`,
            success:ret=>{
                that.wrongAudioSrc=ret.filename;
                that.wrongAudioCtx.src=that.wrongAudioSrc;
            }
        })
        wx.request({
            url:`${app.globalData.url}/cognizes/${test_Classify}`,   
            success:res=>{
                if(res.statusCode==200){
                    that.testList=res.data;
                    that.setData({
                        currentIndex:0,
                        test:that.testList[0],
                        amount:options.amount
                    });
                    that.createAudioSrc(that.data.test)
                }
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        this.questionAudioCtx.pause();
        this.correctAudioCtx.pause();
        this.wrongAudioCtx.pause();
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    towardsLeft:function(){
        this.setData({
            currentIndex:this.data.currentIndex-1,
            test:this.testList[this.data.currentIndex-1]})
        this.createAudioSrc(this.data.test)
    },

    towardsRight:function(){
        this.setData({
            currentIndex:this.data.currentIndex+1,
            test:this.testList[this.data.currentIndex+1]})
        this.createAudioSrc(this.data.test)
    },

    playQuestion:function(){
        this.questionAudioCtx.play();
    },

    answerQuestion:function(e){
        let chooseResult=e.target.id;
        let answer=this.data.test.right_Answer;
        if(chooseResult==answer){
            this.setData({
                showAnswer:'/icon/correct.png',
                isShow:false
            })
            this.correctAudioCtx.play()
            setTimeout(()=>{
                if(this.data.currentIndex+1<this.data.amount){
                    this.setData({
                        currentIndex:this.data.currentIndex+1,
                        test:this.testList[this.data.currentIndex+1],
                        isShow:true
                    })
                    this.createAudioSrc(this.data.test)
                }else{
                    this.setData({currentIndex:this.data.currentIndex+1})
                }
            },2500)
        }else{
            this.setData({
                showAnswer:'/icon/error.png',
                isShow:false
            })
            this.wrongAudioCtx.play()
            setTimeout(()=>{
                this.setData({
                    isShow:true
                })
            },2500)
        }
    },
    testAgain:function(){
        this.setData({
            currentIndex:0,
            test:this.testList[0],
            isShow:true
        })
        this.createAudioSrc(this.data.test)
    },
    exitTest:function(){
        wx.navigateBack({
          delta: 1,
        })
    }

})